<template>
  <a-card style="position: relative;margin:0;border-radius:0px">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="征求意见开始时间">
              <j-date style="width: 100%" v-model="model.startDate"
                      :disabled="(stage == '05010' || stage == '05050') ? true:(bgStatus === '9' || bgStatus === '2' || bgStatus == '3'?true:false)"></j-date>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="征求意见结束时间">
              <j-date style="width: 100%" v-model="model.endDate"
                      :disabled="(stage == '05010' || stage == '05050') ? true:(bgStatus === '9' || bgStatus === '2' || bgStatus == '3'?true:false)"></j-date>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="延期结束时间">
              <j-date style="width: 100%" v-model="model.delayDate"
                      :disabled="(stage == '05010' || stage == '05050') ? true:(bgStatus === '9' || bgStatus === '2' || bgStatus == '3'?true:false)"></j-date>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 操作按钮区域 -->
    <div class="table-operator" v-if="stage !== '05010' && stage !== '05050'">
      <a-button :disabled="bgStatus === '9' || bgStatus === '2' || bgStatus == '3'"
                type="primary"
                @click="saveZqyjg('yj')">保存
      </a-button>
      <a-button :disabled="bgStatus === '9' || bgStatus === '2' || bgStatus == '3'"
                v-permission="['30010']"
                type="primary"
                @click="showConfirm('101')">
        提交审核
      </a-button>
      <a-button :disabled="bgStatus !== '2'"
                v-permission="['30020']"
                @click="setApproveReason('9','101')">
        同意
      </a-button>
      <a-button :disabled="bgStatus !== '2'"
                v-permission="['30020']"
                type="danger"
                @click="setApproveReason('1','101')">
        拒绝
      </a-button>
    </div>
    <div class="ant-alert ant-alert-info" style="margin: 16px 0;">
      <span>征求意见稿</span>
    </div>
    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns1"
        :dataSource="dataSource1"
        :pagination="false"
        :loading="loading"
      >
        <span slot="actionName">
          <a-space v-if="stage !== '05010' && stage !== '05050'" v-permission="['30010']">
            <a-button size="small" @click="downLoadTemplete()" type="primary" icon="download">
              模板下载
            </a-button>
            <a-upload v-if="stage !== '05010' && stage !== '05050'"
                      :disabled="bgStatus === '9' || bgStatus === '2' || bgStatus == '3'"
                      v-permission="['30010']" accept=".docx, .doc, .xlsx, .xls, .pdf, .jpeg, .png"
                      :data="fileData" name="files" :showUploadList="false" :multiple="false"
                      :headers="tokenHeader"
                      :action="url.upLoad" @change="handleImportExcel1">
            <a-button
              size="small"
              type="primary"
              icon="import">上传</a-button>
            </a-upload>
          </a-space>
          <span v-else>操作</span>
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="OnPerview(record)">预览</a>
          <a-divider type="vertical"/>
          <a @click="OnDownLoad(record)">下载</a>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 反馈 -->
    <div v-if="bgStatus === '9'" style='margin-top:50px'>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="6" :sm="8">
              <a-form-item label="反馈时间">
                <j-date style="width: 100%" v-model="model.fkDate"
                        :disabled="fkStatus === '9' || fkStatus === '2'"></j-date>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 操作按钮区域 -->
      <div class="table-operator" v-if="stage !== '05010' && stage !== '05050'">
        <span v-if="fkStatus != '9' && fkStatus != '2'">
          <a-button :disabled="fkStatus === '9' || fkStatus === '2'"
                    type="primary"
                    @click="saveZqyjg('fk')">
            保存
          </a-button>
          <a-button :disabled="fkStatus === '9' || fkStatus === '2'"
                    v-permission="['30010']"
                    type="primary"
                    @click="showConfirm('102')">
            提交审核
          </a-button>
        </span>
        <a-button :disabled="fkStatus !== '2' || fkAudit !== '2'"
                  v-permission="['30020']"
                  @click="setApproveReason('9','102')">
          同意
        </a-button>
        <a-button :disabled="fkStatus !== '2' || fkAudit !== '2'"
                  v-permission="['30020']"
                  type="danger"
                  @click="setApproveReason('1','102')">
          拒绝
        </a-button>
      </div>
      <div class="ant-alert ant-alert-info" style="margin: 16px 0;">
        <span>反馈</span>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns2"
        :dataSource="dataSource2"
        :pagination="false"
        :loading="loading"
      >
        <span slot="actionName">
          <a-space v-if="stage !== '05010' && stage !== '05050'" v-permission="['30010']">
            <a-button size="small" @click="downLoadTemplete()" type="primary" icon="download">
              模板下载
            </a-button>
            <a-upload v-if="stage !== '05010' && stage !== '05050'"
                      :disabled="fkStatus === '9' || fkStatus === '2'"
                      v-permission="['30010']" accept=".docx, .doc, .xlsx, .xls, .pdf, .jpeg, .png"
                      :data="fileData2" name="files" :showUploadList="false" :multiple="false"
                      :headers="tokenHeader"
                      :action="url.upLoad" @change="handleImportExcel2">
              <a-button size="small"
                        type="primary"
                        icon="import">上传</a-button>
            </a-upload>
          </a-space>
          <span v-else>操作</span>
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="OnPerview(record)">预览</a>
          <a-divider type="vertical"/>
          <a @click="OnDownLoad(record)">下载</a>
        </span>
      </a-table>
    </div>
    <SelectApprove ref='approver' :id='id' @submitData='handleSubmit'></SelectApprove>
    <SetReason ref='setReason' @approveReason='approveData'></SetReason>
    <TempleteDownload ref="templeteDownload" :type="tempType" :url="url.templateUrl" />
  </a-card>
</template>
<script>

  import {getAction, postAction} from '@/api/manage'
  import columns1 from './indexColumns'
  import columns2 from './workColumns'
  import SelectApprove from '../public/SelectApprover'
  import SetReason from "../public/SetReason"
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import {filePerviewMixin} from '@/mixins/filePerviewMixin'
  import TempleteDownload from '@views/sj/components/TempleteDownload.vue'

  export default {
    props: ['id', 'stage'],
    name: 'zqyjg',
    mixins: [JeecgListMixin, filePerviewMixin],
    components: {
      TempleteDownload,
      SelectApprove,
      SetReason
    },
    data() {
      return {
        queryParam: {
          id: this.id
        },
        model: {},
        loading: false,
        fileData: {
          biz: 'baogao_zqyj',
          type: 'baogao_zqyj',
          proId: this.id
        },
        fileData2: {
          biz: 'baogao_fk',
          type: 'baogao_fk',
          proId: this.id
        },
        // 表头
        columns1: columns1(this),
        columns2: columns2(this),
        dataSource1: [],
        dataSource2: [],
        bgStatus: '',
        fkStatus: '',
        bgAudit: '',
        fkAudit: '',
        approveStatus: '',
        typeId: '',
        bgType: 'baogao_zqyj',
        fkType: 'baogao_fk',
        url: {
          save: '/sjxmzygl/sjWorkBaogaoZqyj/save',
          saveBg: '/sjxmzygl/sjWorkBaogao/saveBg',
          submit: '/sjxmzygl/sjWorkUsers/submitAudit',
          getAudit: 'sjxmzygl/sjWorkUsers/queryAuditAuth',
          approve: '/sjxmzygl/sjWorkUsers/zyApplyCommit',
          list: '/sjxmzygl/sjWorkBaogao/queryAuditReportInfo',
          yjData: '/sjxmzygl/sjWorkBaogaoZqyj/queryBgZqyjInfo',
          templateUrl: '/xtsz/sjWordTemplate/list',
          upLoad: `${window._CONFIG['domianURL']}/sys/common/sjBgUpload`
        },
        tempType: 'baogao_zqyj'
      }
    },
    created() {
      this.getYjData()
      this.getAudit()
    },
    methods: {
      loadData() {
        this.dataSource1 = []
        this.dataSource2 = []
        this.loading = true
        getAction(this.url.list, this.queryParam).then((res) => {
          if (res.success) {
            this.bgStatus = res.result.zqyj.status
            this.fkStatus = res.result.fk.status
            if (res.result.zqyj.id != undefined) {
              this.model.fileId = res.result.zqyj.id
              this.dataSource1.push(res.result.zqyj)
            }
            if (res.result.fk.id != undefined) {
              this.dataSource2.push(res.result.fk)
            }
          } else {
            this.$message.error(res.message)
          }
          if (res.code === 510) {
            this.$message.warning(res.message)
          }
          this.loading = false
        })
      },
      getAudit() {
        getAction(this.url.getAudit, {proId: this.id}).then((res) => {
          if (res.success) {
            res.result.map((item) => {
              if (item.id === '101') {
                this.bgAudit = item.status
              } else if (item.id === '102') {
                this.fkAudit = item.status
              }
            })
          } else {
            this.$message.error(res.message)
          }
        })
      },
      // 获取头部信息
      getYjData() {
        getAction(this.url.yjData, this.queryParam).then((res) => {
          if (res.success) {
            if (res.result.length != 0) {
              this.model = res.result[0]
            }
          } else {
            this.$message.error(res.message)
          }
          if (res.code === 510) {
            this.$message.warning(res.message)
          }
          this.loading = false
        })
      },
      handleImportExcel1(info) {
        if (info.file.status === 'done') {
          if (info.file.response.success) {
            this.$message.success(info.file.response.message || `${info.file.name} 文件上传成功`)
            this.model.fileId = info.file.response.result[0].id
            this.saveBg(info.file.response.result[0].id, this.bgType)
          } else {
            this.$message.error(`${info.file.name} ${info.file.response.message}.`);
          }
        } else if (info.file.status === 'error') {
          this.$message.error(`文件上传失败: ${info.file.msg} `);
        }
      },
      handleImportExcel2(info) {
        if (info.file.status === 'done') {
          if (info.file.response.success) {
            this.$message.success(info.file.response.message || `${info.file.name} 文件上传成功`)
            this.saveBg(info.file.response.result[0].id, this.fkType)
          } else {
            this.$message.error(`${info.file.name} ${info.file.response.message}.`);
          }
        } else if (info.file.status === 'error') {
          this.$message.error(`文件上传失败: ${info.file.msg} `);
        }
      },
      saveZqyjg(type) {
        if (type = 'yj') {
          if (this.model.startDate === null || this.model.startDate === undefined || this.model.startDate === '') {
            this.$message.warning('请选择征求意见开始时间!')
            return
          }
          if (this.model.endDate === null || this.model.endDate === undefined || this.model.endDate === '') {
            this.$message.warning('请选择征求意见结束时间!')
            return
          }
        }
        if (type === 'fk') {
          if (this.model.fkDate === null || this.model.fkDate === undefined || this.model.fkDate === '') {
            this.$message.warning('请选择反馈日期!')
            return
          }
        }
        postAction(this.url.save, {
          proId: this.id,
          ...this.model
        }).then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.loadData()
            this.getAudit()
          } else {
            this.$message.error(res.message)
          }
        })
      },
      // 上传文件时调的保存接口
      saveBg(fileId, bgType) {
        postAction(this.url.saveBg, {
          proId: this.id,
          fileId: fileId,
          type: bgType
        }).then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.loadData()
            this.getAudit()
          } else {
            this.$message.error(res.message)
          }
        })
      },
      showConfirm(typeId) {
        if (typeId === '101' && this.dataSource1.length == 0) {
          this.$message.warning("请先上传文件!")
          return
        }
        if (typeId === '102' && this.dataSource2.length == 0) {
          this.$message.warning("请先上传文件!")
          return
        }
        this.typeId = typeId
        this.$refs.approver.showApproveConfirm = true
      },
      handleSubmit(userId) {
        getAction(this.url.submit, {
          proId: this.id,
          userId: userId,
          typeId: this.typeId
        }).then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.loadData()
            this.getAudit()
          }
        })
      },
      setApproveReason(status, typeId) {
        this.approveStatus = status
        this.typeId = typeId
        this.$refs.setReason.setApproveReason = true
      },
      approveData(reason) {
        let remark = this.approveStatus === '9' ? `同意：${reason}` : `拒绝：${reason}`
        getAction(this.url.approve, {
          proId: this.id,
          status: this.approveStatus,
          remark: remark,
          typeId: this.typeId
        }).then((res) => {
          if (res.success) {
            this.$refs.setReason.setApproveReason = false
            this.loadData()
            this.getAudit()
          } else {
            this.$message.error(res.message)
          }
        })
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
<style lang="less" scoped>

  .ant-row {
    height: 100%;

    .ant-col {
      height: 100%;

      .ant-card {
        height: 100%;

        /deep/ .ant-card-body {
          padding: 15px;
        }
      }
    }
  }

  .funBtn {
    padding: 0 10px;
    margin-left: 10px;
  }

</style>